package cn.yuhh.controller;

import cn.yuhh.pojo.Brand;
import cn.yuhh.pojo.PageResult;
import cn.yuhh.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

//请求URL
//http://api.leyou.com/api/item/brand/page?page=1&rows=5&sortBy=id&desc=false&search=
@Controller
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> queryBrandsByPage(
            @RequestParam(name = "key",required = false) String key,
            @RequestParam(name = "page",defaultValue = "1") Integer page,
            @RequestParam(name="rows",defaultValue = "5") Integer rows,
            @RequestParam(name = "sortBy",required = false) String sortBy,
            @RequestParam(name="desc",required = false) Boolean desc) {

        PageResult<Brand> brandPageResult = this.brandService.queryBrandsByPage(key, page, rows, sortBy, desc);
        if (brandPageResult == null) {
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(brandPageResult);

    }
}
